package com.cloud.phone.tools;


import com.cloud.phone.view.TsUtils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class CmdTool {

    private volatile static CmdTool tool;

    /**
     * 获取单例
     *
     * @return
     */
    public static CmdTool getInstance() {
        if (tool == null) {
            synchronized (CmdTool.class) {
                if (tool == null) {
                    tool = new CmdTool();
                }
            }
        }
        return tool;
    }


    //使用root权限执行命令 执行adb shell命令
    public boolean doCmd(String cmd) {
        try {
            OutputStream os;
            os = Runtime.getRuntime().exec("su").getOutputStream();
            os.write(cmd.getBytes());
            os.flush();//清空缓存
            os.close();//停止流
            LogUtils.e("命令执行完成");
            return true;
        } catch (Exception e) {
            LogUtils.e(e.getMessage());
            LogUtils.v("命令出错了:" + e.getMessage());
        }
        return false;
    }


    public void getAccPermission(String command) {
       // String command = "adb shell pm grant " + BaseApp.app.getPackageName() + " android.permission.WRITE_SECURE_SETTINGS";
        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuilder output = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }
            reader.close();
            process.waitFor();
            String result = output.toString();
            LogUtils.d("===执行结果3333===" + result);
            // 处理命令执行结果
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    /**
     * 关机重启
     */
    public void reOpenPhone() {
        LogUtils.v("关机重启");
        String cmd = "reboot";
        doCmd(cmd);
    }


    /**
     * 点击返回按钮
     */
    public void forceBack() {
        LogUtils.v("点击返回键");
        String cmd = "input keyevent 4";
        doCmd(cmd);
    }

    public void killApp(String packageName){
        doCmd("am force-stop "+packageName);
    }


    public void swipeByCmd(int x, int y, int lastX, int lastY) {
        //上滑列表，状态改为查找头像进行回访
        String move = "input swipe " + x + " " + y + " " + lastX + " " + lastY;
        LogUtils.v("上滑");
        doCmd(move);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public boolean isDeviceRooted() {
        // 通过执行shell命令来判断设备是否已经获取Root权限
        String buildTags = android.os.Build.TAGS;
        if (buildTags != null && buildTags.contains("test-keys")) {
            return true;
        }

        // 通过执行su命令来判断设备是否已经获取Root权限
        try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            return process.exitValue() == 0;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean requestRootAccess() {
        try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            return process.exitValue() == 0;
        } catch (Exception e) {
            return false;
        }
    }

    public void executeShellCommand(String command) {
        try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuilder output = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }
            String result = output.toString();
            TsUtils.showTips("执行结果"+result);
            LogUtils.d("===执行结果2222===" + result);
        } catch (Exception e) {
            e.printStackTrace();
            TsUtils.showTips("执行结果:失败");
            LogUtils.d("===执行结果1111===" + e.getMessage());
        }
    }

}
